import 'dart:io';

import 'package:bwt_ad_flutter/ad/BwtAd.dart';
import 'package:bwt_ad_flutter_example/util/Log.dart';
import 'package:bwt_ad_flutter_example/widget/SimpleWidget.dart';
import 'package:flutter/material.dart';
import 'package:bwt_ad_flutter/ad/loader/BwtInterstitialAdLoader.dart';
import 'package:fluttertoast/fluttertoast.dart';

class InterstitialAdPage extends StatefulWidget {
  const InterstitialAdPage({super.key});

  @override
  State<StatefulWidget> createState() => _InterstitialAdPageState();
}

class _InterstitialAdPageState extends State<InterstitialAdPage> {
  final BwtInterstitialAdLoader _interstitialAdLoader =
      BwtInterstitialAdLoader();
  BwtAd? _bwtAd;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("插屏广告"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 加载插屏广告
          _buildBtn(
            title: '加载插屏广告',
            onTap: () {
              _loadInterstitialAd(loadThenShow: false);
            },
          ),

          // 展示插屏广告
          const Padding(padding: EdgeInsets.only(top: 24)),
          _buildBtn(
            title: '展示插屏广告',
            onTap: () {
              _showInterstitialAd();
            },
          ),

          // 加载并展示插屏广告
          const Padding(padding: EdgeInsets.only(top: 24)),
          _buildBtn(
            title: '加载并展示插屏广告',
            onTap: () {
              _loadInterstitialAd(loadThenShow: true);
            },
          ),
        ],
      ),
    );
  }

  Widget _buildBtn({
    required String title,
    required GestureTapCallback onTap,
  }) =>
      SimpleWidget(
        alignment: Alignment.center,
        width: double.infinity,
        margin: const EdgeInsets.symmetric(horizontal: 32),
        height: 48,
        decoration: BoxDecoration(
          color: Colors.blue,
          borderRadius: BorderRadius.circular(4),
        ),
        onTap: onTap,
        child: Text(
          title,
          style: const TextStyle(
            color: Colors.white,
            fontSize: 16,
          ),
        ),
      );

  @override
  void initState() {
    super.initState();

    // 设置广告加载成功监听
    _interstitialAdLoader.onAdLoad = (bwtAd) {
      _bwtAd = bwtAd;
      Fluttertoast.showToast(msg: "插屏广告加载成功");
      Log.d('插屏广告加载成功...');
    };

    // 设置广告加载失败监听
    _interstitialAdLoader.onAdFailed = (msg) {
      Fluttertoast.showToast(msg: "插屏广告加载失败");
      Log.d('插屏广告加载失败, $msg');
    };

    // 设置广告展示监听
    _interstitialAdLoader.onAdExpose = (bwtAd) {
      Log.d('插屏广告展示成功...');
    };

    // 设置广告被点击监听
    _interstitialAdLoader.onAdClick = (bwtAd) {
      Log.d('插屏广告被点击...');
    };

    // 设置广告关闭监听
    _interstitialAdLoader.onAdClose = (bwtAd) {
      Log.d('插屏广告被关闭...');
    };
  }

  @override
  void dispose() {
    /// 释放广告
    _interstitialAdLoader.release();
    super.dispose();
  }

  /// 加载插屏广告
  /// loadThenShow 广告加载成功后是否立即展示
  void _loadInterstitialAd({required bool loadThenShow}) {
    // 加载插屏广告
    _interstitialAdLoader.loadAd(
      // 广告位ID，不同端的广告位ID可能不一致，需替换成自己相应端的广告位ID
      posId: Platform.isAndroid ? '21341013' : '70774478',
      // 加载成功后是否立即展示，如果设置为false，请在onAdLoad回调中保存bwtAd对象，在适当时机调用BwtInterstitialAdLoader的showAd方法
      loadThenShow: loadThenShow,
    );
  }

  /// 展示插屏广告（同一个插屏广告（bwtAd）只能展示一次）
  void _showInterstitialAd() {
    if (_bwtAd != null) {
      _interstitialAdLoader.showAd(bwtAd: _bwtAd!);
    }
  }
}
